﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Dycox
{
    public static class LinqToXmlHelper
    {
        public static string GetAttrString(this XElement elt, XName name)
        {
            return (string)elt.Attribute(name);
        }

        public static DateTime? GetAttrDateTime(this XElement elt, XName name)
        {
            return (DateTime?)elt.Attribute(name);
        }

        public static T GetAttrEnum<T>(this XElement elt, XName name)
        {
            return GetAttrEnum<T>(elt, name, default(T));
        }

        public static T GetAttrEnum<T>(this XElement elt, XName name, T defaultValue)
        {
            string value = (string)elt.Attribute(name);

            if (!string.IsNullOrEmpty(value))
            {
                return (T)Enum.Parse(typeof(T), value, true);
            }
            else
                return defaultValue;
        }

        public static bool? GetAttrBoolean(this XElement elt, XName name)
        {
            return (bool?)elt.Attribute(name);
        }

        public static int? GetAttrInt32(this XElement elt, XName name)
        {
            return (int?)elt.Attribute(name);
        }

        public static float? GetAttrSingle(this XElement elt, XName name)
        {
            return (float?)elt.Attribute(name);
        }

    }
}
